
class_name CMailDes

extends EventDispatcher

signal UUID_changed(old_value, new_value) 
signal ToCharId_changed(old_value, new_value) 
signal FromCharId_changed(old_value, new_value) 
signal FromCharDisplayName_changed(old_value, new_value) 
signal Mailid_changed(old_value, new_value) 
signal Flags_changed(old_value, new_value) 
signal TimeSent_changed(old_value, new_value) 
signal Expiry_changed(old_value, new_value) 
signal Subject_changed(old_value, new_value) 
signal Content_changed(old_value, new_value) 
signal SubjectParam_changed(old_value, new_value) 
signal ContentParam_changed(old_value, new_value) 
signal Attachment_changed(old_value, new_value) 
signal NeedLang_changed(old_value, new_value) 
signal NeedNation_changed(old_value, new_value) 
signal NeedPlatform_changed(old_value, new_value) 


var m_tblAlias
var bInited = false
func _init(_uuid)->void:
    self.UUID = _uuid
    self.ToCharId = 0
    self.FromCharId = 0
    self.FromCharDisplayName = ''
    self.Mailid = 0	# 邮件配置表id，0 表示Subject，Content 是输入的，否则从g_tbConfig.Email 中读取
    self.Flags = 0	# MailboxMessageFlags 邮件状态
    self.TimeSent = 0
    self.Expiry = 0	# 过期时间，0表示永不过期
    self.Subject = ''
    self.Content = ''
    self.SubjectParam = ''	# Subject参数
    self.ContentParam = ''	# Content参数
    self.Attachment = ''
    self.NeedLang = ''	# 限制语言
    self.NeedNation = ''	# 限制国家
    self.NeedPlatform = ''	# 限制渠道

    self.m_tblAlias = {
 
    }

func UnPack(_bufferStream):
    self.UUID = _bufferStream.ReadInt64()
    self.ToCharId = _bufferStream.ReadInt64()
    self.FromCharId = _bufferStream.ReadInt64()
    self.FromCharDisplayName = _bufferStream.ReadString(TableCommonDefine.ECharLen.eCL_FromCharDisplayName)
    self.Mailid = _bufferStream.ReadInt32()
    self.Flags = _bufferStream.ReadUint16()
    self.TimeSent = _bufferStream.ReadUint32()
    self.Expiry = _bufferStream.ReadUint32()
    self.Subject = _bufferStream.ReadString(TableCommonDefine.ECharLen.eCL_Subject)
    self.Content = _bufferStream.ReadString(TableCommonDefine.ECharLen.eCL_Content)
    self.SubjectParam = _bufferStream.ReadBytes(TableCommonDefine.ECharLen.eCL_SubjectParam)
    self.ContentParam = _bufferStream.ReadBytes(TableCommonDefine.ECharLen.eCL_ContentParam)
    self.Attachment = _bufferStream.ReadBytes(TableCommonDefine.ECharLen.eCL_Attachment)
    self.NeedLang = _bufferStream.ReadString(TableCommonDefine.ECharLen.eCL_NeedLang)
    self.NeedNation = _bufferStream.ReadString(TableCommonDefine.ECharLen.eCL_NeedNation)
    self.NeedPlatform = _bufferStream.ReadString(TableCommonDefine.ECharLen.eCL_NeedPlatform)

    self.bInited = true


func GetTableName():
    return 'MailDes'

func CopyFrom(other):
    self.ToCharId = other.ToCharId
    self.FromCharId = other.FromCharId
    self.FromCharDisplayName = other.FromCharDisplayName
    self.Mailid = other.Mailid
    self.Flags = other.Flags
    self.TimeSent = other.TimeSent
    self.Expiry = other.Expiry
    self.Subject = other.Subject
    self.Content = other.Content
    self.SubjectParam = other.SubjectParam
    self.ContentParam = other.ContentParam
    self.Attachment = other.Attachment
    self.NeedLang = other.NeedLang
    self.NeedNation = other.NeedNation
    self.NeedPlatform = other.NeedPlatform


func OnDataChanged( fieldName, oldValue, newValue):
    if oldValue == newValue:
        return
    
    self.DispatchEvent(fieldName, [self, oldValue, newValue])
    #  Alias
    if self.m_tblAlias[fieldName] != null:
        for i in range(self.m_tblAlias[fieldName].size()):
            self.DispatchEvent(self.m_tblAlias[fieldName][i], [self, oldValue, newValue])

func UnPackField(_fieldId, _bufferStream, _varLen ):
    if _fieldId < 0 or _fieldId == 0xffff:
        var old = CMailDes.new(self.m_UUID)
        old.CopyFrom( self )
        self.UnPack( _bufferStream )
        self.DispatchEvent("CMailDes", [self, old, self])
        return true

    print("UnPackField error tblName = MailDes, className = CMailDes, _fieldId = ", _fieldId)
    # if _fieldId > Fields_MailDesID.size():
    #     var szLog = 'CMailDes.UnPackField  _fieldId(%d) < 0 or _fieldId(%d) > #Fields_MailDesID(%d)' % [_fieldId, _fieldId,  Fields_MailDesID.size()]
    #     print(szLog)
    #     return false

    # Fields_MailDesID[_fieldId].reader( self, _bufferStream, _varLen)

    return true

func AddField(_fieldId, _bufferStream):
    print( "[TABLEDATA] error AddField", "CMailDes", _fieldId )

    
func DelField(_fieldId ):
    print( "[TABLEDATA] error DelField", "CMailDes", _fieldId )

var UUID:
    set(value):
        var old = self.m_UUID
        self.UUID = _value
        self.UUID_changed.emit(old, _value)

    get:
        return self.UUID


var ToCharId:
    set(value):
        var old = self.m_ToCharId
        self.ToCharId = _value
        self.ToCharId_changed.emit(old, _value)

    get:
        return self.ToCharId


var FromCharId:
    set(value):
        var old = self.m_FromCharId
        self.FromCharId = _value
        self.FromCharId_changed.emit(old, _value)

    get:
        return self.FromCharId


var FromCharDisplayName:
    set(value):
        var old = self.m_FromCharDisplayName
        self.FromCharDisplayName = _value
        self.FromCharDisplayName_changed.emit(old, _value)

    get:
        return self.FromCharDisplayName


var Mailid:
    set(value):
        var old = self.m_Mailid
        self.Mailid = _value
        self.Mailid_changed.emit(old, _value)

    get:
        return self.Mailid


var Flags:
    set(value):
        var old = self.m_Flags
        self.Flags = _value
        self.Flags_changed.emit(old, _value)

    get:
        return self.Flags


var TimeSent:
    set(value):
        var old = self.m_TimeSent
        self.TimeSent = _value
        self.TimeSent_changed.emit(old, _value)

    get:
        return self.TimeSent


var Expiry:
    set(value):
        var old = self.m_Expiry
        self.Expiry = _value
        self.Expiry_changed.emit(old, _value)

    get:
        return self.Expiry


var Subject:
    set(value):
        var old = self.m_Subject
        self.Subject = _value
        self.Subject_changed.emit(old, _value)

    get:
        return self.Subject


var Content:
    set(value):
        var old = self.m_Content
        self.Content = _value
        self.Content_changed.emit(old, _value)

    get:
        return self.Content


var SubjectParam:
    set(value):
        var old = self.m_SubjectParam
        self.SubjectParam = _value
        self.SubjectParam_changed.emit(old, _value)

    get:
        return self.SubjectParam


var ContentParam:
    set(value):
        var old = self.m_ContentParam
        self.ContentParam = _value
        self.ContentParam_changed.emit(old, _value)

    get:
        return self.ContentParam


var Attachment:
    set(value):
        var old = self.m_Attachment
        self.Attachment = _value
        self.Attachment_changed.emit(old, _value)

    get:
        return self.Attachment


var NeedLang:
    set(value):
        var old = self.m_NeedLang
        self.NeedLang = _value
        self.NeedLang_changed.emit(old, _value)

    get:
        return self.NeedLang


var NeedNation:
    set(value):
        var old = self.m_NeedNation
        self.NeedNation = _value
        self.NeedNation_changed.emit(old, _value)

    get:
        return self.NeedNation


var NeedPlatform:
    set(value):
        var old = self.m_NeedPlatform
        self.NeedPlatform = _value
        self.NeedPlatform_changed.emit(old, _value)

    get:
        return self.NeedPlatform



